 package com.quanyan.common.utils;

import com.quanyan.api.APIResponse;

import java.util.regex.Pattern;

/**
 * @Author kingson.chan
 * @Since 2016-04-19
 */
public class ValidatorUtils<T> {
	/**
	 *
	 * 校验密码
	 *
	 * @return
	 */
	public static APIResponse pw(String pw, String lan) {
		if (pw == null || pw.equals("")) {
			return APIResponse.returnFail("密码不能为空");
		}
		if (pw.length() < 1 || pw.length() > 16) {
			return APIResponse.returnFail("密码长度不正确");
		}
		return null;
	}

	/**
	 * 校验电话号码
	 *
	 * @return
	 */
	public static APIResponse phone(String phone) {
		if (phone != null) {
			if (!phone.matches("[0-9]{5,20}")) {
				return APIResponse.returnFail("手机格式有问题，或者长度有问题！");
			}
		}

		return null;
	}


	/**
	 * 长度校验
	 * @param content
	 * @param title
	 * @param length
	 * @return
	 */
	public static APIResponse lengthValidate(String content,String title,int length) {
		if(content != null && !"".equals(content)){
			if (content.length() > length) {
				return APIResponse.returnFail(title + "的长度必须小于" + length +"个字符！");
			}
		}
		return null;
	}

	public static APIResponse lengthValidateMustInput(String content,String title,int length) {
		if (content == null || content.equals("")) {
			return APIResponse.returnFail( title + "不能为空");
		}
		if (content.length() > length) {
			return APIResponse.returnFail(title + "的长度必须小于" + length +"个字符！");
		}
		return null;
	}


	public static APIResponse isNum(String num,String title) {
		if (num != null) {
			if (!num.matches("^[0-9]*$")) {
				return APIResponse.returnFail(title + "必须为数字。");
			}
		}
		return null;
	}

	public static APIResponse isNumAndMustInput(String num,String title,int max) {
		if (num == null || num.equals("")) {
			return APIResponse.returnFail(title + "不能为空");
		}
		if (!num.matches("^[0-9]*$")) {
			return APIResponse.returnFail(title + "必须为数字。");
		}
		else {
			if (Integer.parseInt(num) > max){
				return APIResponse.returnFail(title + "必须小于"+ max);
			}
		}
		return null;
	}


	public static APIResponse isLat(String num) {
		if (num != null) {
			if (!num.matches("^[-]?(\\d|([1-8]\\d)|(90))(\\.\\d*)?$")) {
				return APIResponse.returnFail("纬度格式不正确。");
			}
		}
		return null;
	}

	public static APIResponse isLng(String num) {
		if (num != null) {
			if (!num.matches("^[-]?(\\d|([1-9]\\d)|(1[0-7]\\d)|(180))(\\.\\d*)?$")) {
				return APIResponse.returnFail("经度格式不正确。");
			}
		}
		return null;
	}

	public  static APIResponse decimal(String str,String title) {//判断字符串为浮点数
		if (str != null){
			Pattern pattern = Pattern.compile("[0-9]*(\\.?)[0-9]*");
			if(!pattern.matcher(str).matches()){
				return APIResponse.returnFail(title +"必须为数字或者小数。");
			}
		}
		return null;
    }

	public static APIResponse isYear(String str){
		if (str != null) {
			if (!str.matches("^[1-2][0-9]{3}")) {
				return APIResponse.returnFail("开业时间年份输入格式不对。");
			}
		}
		return null;
	}

	public static APIResponse isTime(String str,String title){
		if (str == null || str.equals("")) {
			return APIResponse.returnFail(title + "不能为空");
		}
		if (!str.matches("^([0-1][0-9]|2[0-4]):[0-5][0-9](:[0-5][0-9])?")) {
			return APIResponse.returnFail(title + "格式不对。");
		}
		return null;
	}

	public static void main(String[] args) {
		System.out.println(isTime("23:59:59", "111"));
		//System.out.println(email("zhuo@yahoo.com"));
		/*String num = "013-12345678";
		if(num.matches("\\(?0\\d{2}[)-]+\\d{8}")){//匹配电话号码
			System.out.println("YES");
		}
		else {
			System.out.println("NO");
		}*/
		/*if(num.matches("((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)")){//匹配ip
			System.out.println("YES");
		}
		else {
			System.out.println("NO");
		}
		num = "www www";
		if(num.matches("\\b(\\w+)\\b\\s+\\1\\b")){//后项引用
			System.out.println("YES");
		}
		else {
			System.out.println("NO");
		}*/
		/*num = "hao@123";
		if(num.matches("")){//后项引用
			System.out.println("YES");
		}
		else {
			System.out.println("NO");
		}*/

		String str = "2004";
		System.out.println(str.matches("^[1-2][0-9]{3}"));
	}

}
